﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;

namespace YouHP.Pages.Profile
{
    public class SymptomAjax : BasePage
    {
        private int showMode = -1;
        
        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            if (IsPost == false) HandleGet();
        }

        protected void HandleGet()
        {
            int id = RequestTools.GetQueryInt("id", 0);  
            showMode = RequestTools.GetQueryInt("showMode", Constants.DEFAULTMODE);
            
            if (id <= 0) return;
            PatientRecordInfo prData = PatientBLL.Instance.GetPatientRecordInfo(id);
            if (prData.Id <= 0) return;
            MakeGraphData(prData);
        }

        private void MakeGraphData(PatientRecordInfo prData)
        {
            int primaryCnt = 0, secondCnt = 0, lastId = -1, groupCount = 0;
            SymptomHistoryInfo item = null;
            string tmpTimeStr = "", tmpValueStr = "";
            StringBuilder sb = new StringBuilder();

            sb.Append("&Symptom=1&\r\n");
            sb.AppendFormat("&ShowMode={0}&\r\n", showMode);
            sb.AppendFormat("&StartDate={0:yyyy-MM-dd}&\r\n", prData.StartDate);
            sb.AppendFormat("&DosageDate={0:yyyy-MM-dd}&\r\n", prData.ConfirmDate);
            sb.AppendFormat("&UpdateDate={0:yyyy-MM-dd}&\r\n", prData.UpdateDate);            

            List<SymptomHistoryInfo> lst = PatientSymptomBLL.Instance.GetSymptomHistoryList(prData.Id, Constants.ALLMODE);
            for (int i = 0; i < lst.Count; i++)
            {
                item = lst[i];

                //如果是一个新的症状开始，则需要回写症状数据
                if (item.SymptomId != lastId && i != 0)
                {
                    tmpTimeStr += "&\r\n";
                    tmpValueStr += "&\r\n";
                    sb.AppendFormat(tmpTimeStr);
                    sb.AppendFormat(tmpValueStr);
                }

                if (item.SymptomId != lastId && item.Type == Constants.PRIMARY_SYMPTOM)
                {
                    primaryCnt++;
                    sb.AppendFormat("&Var_1_{0}={1}&\r\n", primaryCnt, item.SymptomName);
                    tmpTimeStr = string.Format("&Time_1_{0}={1}", primaryCnt, item.RecordDate.ToString("yyyy-MM-dd"));
                    tmpValueStr = string.Format("&Value_1_{0}={1}", primaryCnt, item.DegreeValue);
                }
                else if (item.SymptomId != lastId && item.Type == Constants.SECOND_SYMPTOM)
                {
                    secondCnt++;
                    sb.AppendFormat("&Var_2_{0}={1}&\r\n", secondCnt, item.SymptomName);
                    tmpTimeStr = string.Format("&Time_2_{0}={1}", secondCnt, item.RecordDate.ToString("yyyy-MM-dd"));
                    tmpValueStr = string.Format("&Value_2_{0}={1}", secondCnt, item.DegreeValue);
                }
                else if (item.SymptomId == lastId)
                {
                    tmpTimeStr += string.Format(";{0}", item.RecordDate.ToString("yyyy-MM-dd"));
                    tmpValueStr += string.Format(";{0}", item.DegreeValue);
                }

                lastId = item.SymptomId;
            }

            //对于最后一个
            if (lst.Count > 0)
            {
                tmpTimeStr += "&\r\n";
                tmpValueStr += "&\r\n";
                sb.AppendFormat(tmpTimeStr);
                sb.AppendFormat(tmpValueStr);
            }

            //输出关于组信息
            sb.Append("\r\n");
            groupCount++;
            sb.AppendFormat("&Group1=主要症状;{0}&\r\n", primaryCnt);

            if (secondCnt > 0)
            {
                groupCount++;
                sb.AppendFormat("&Group2=次要症状;{0}&\r\n", secondCnt);
            }

            sb.AppendFormat("&GroupCount={0}&\r\n", groupCount);

            ResponseXML(sb);
        }

        private void ResponseXML(StringBuilder sb)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ContentType = "Text/html";
            System.Web.HttpContext.Current.Response.Expires = 0;

            System.Web.HttpContext.Current.Response.Write(sb.ToString());
            System.Web.HttpContext.Current.Response.End();
        }
    }
}